/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package admin;

import Utils.Static;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.OrderModel;

/**
 *
 * @author user
 */
public class orders extends HttpServlet {
      boolean isPost = false;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //check if is logged in
            HttpSession session = request.getSession(true);
            //check if is connect
            if (session.getAttribute(Static.adminID) == null) {
                getServletContext().getRequestDispatcher("/admin/login/index.jsp").forward(request, response);
            }

            String action = request.getParameter(Static.action);

            if (action == null) {
                List(request, response);
            } else if (action.compareTo(Static.delete) == 0) {
                Delete(request, response);
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        isPost = false;
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        isPost = true;
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void Delete(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession(true);
            int orderID = Integer.parseInt(request.getParameter(Static.id));

            OrderModel OrderModel = new OrderModel();

            if (!isPost) {
                HashMap order = OrderModel.GetOrder(orderID);
                session.setAttribute(Static.order, order);
                getServletContext().getRequestDispatcher("/admin/orders/delete.jsp").forward(request, response);
                return;
            }

            if (request.getParameter("confirmButton").compareTo("Da")==0) {
			if (OrderModel.DeleteOrder(orderID)==-1) {
				session.setAttribute( Static.error,OrderModel.GetMessage());
			} else {
				session.setAttribute( Static.confirm,OrderModel.GetMessage());
			}
		}

            response.sendRedirect("orders");
        } catch (Exception ex) {
        }
    }

    public void List(HttpServletRequest request, HttpServletResponse response) {
        try {

            HttpSession session = request.getSession(true);

            String sortFields[][] = {{"Username", "User"}, {"OrderID", "Nume Comanda"}, {"Date", "Data"}};
            String sort = sortFields[0][0];
            int pageSize = 10;
            int currPage = 1;
            int firstPage = 1;
            String sortDir = "asc";

            if (request.getParameter(Static.sort) != null) {
                sort = request.getParameter(Static.sort);
            }

            if (request.getParameter(Static.sortDir) != null) {
                sortDir = request.getParameter(Static.sortDir).compareTo("desc") == 0 ? "desc" : "asc";
            }

            if (request.getParameter(Static.currPage) != null) {
                currPage = Integer.parseInt(request.getParameter(Static.currPage));
            }

            if (request.getParameter(Static.firstPage) != null) {
                firstPage = Integer.parseInt(request.getParameter(Static.firstPage));
            }

//calculez numarul de pagini
            OrderModel orderModel = new OrderModel();
            int recordsCount = orderModel.GetOrders_Count();
            int pageCount = (int) (recordsCount / pageSize);

//extrag pagina currPage
            int offset = (currPage - 1) * pageSize;
            ArrayList orders = orderModel.GetOrders(offset, pageSize, sort, sortDir);

//adaug variabilele in sesiune in sesiune

            session.setAttribute(Static.currPage, currPage);
            session.setAttribute(Static.firstPage, firstPage);
            session.setAttribute(Static.pageCount, pageCount);
            session.setAttribute(Static.sort, sort);
            session.setAttribute(Static.sortDir, sortDir);
            session.setAttribute(Static.sortFields, sortFields);
            session.setAttribute(Static.orders, orders);

            getServletContext().getRequestDispatcher("/admin/orders/index.jsp").forward(request, response);

        } catch (Exception ex) {
        }
    }
}
